package com.dongdongshop;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
@RocketMQMessageListener(topic ="code_topic",consumerGroup = "code_grop")
public class SmsRocketMQ implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
            defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                    //获取message
                    try {
                    MessageExt messageExt = list.get(0);
                    byte[] body = messageExt.getBody();
                    String str =new String(body);
                    System.out.println(str);
                    DefaultProfile profile = DefaultProfile.getProfile("cn-beijing",
                            "LTAI4GCbdwnjCGwQj9Z3tr3N", "27ih4pc1W1tqey9X4mmCy1JUotAX7L");
                    /** use STS Token
                     DefaultProfile profile = DefaultProfile.getProfile(
                     "<your-region-id>",           // The region ID
                     "<your-access-key-id>",       // The AccessKey ID of the RAM account
                     "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
                     "<your-sts-token>");          // STS Token
                     **/
                    IAcsClient client = new DefaultAcsClient(profile);

                    CommonRequest request = new CommonRequest();
                    //请求方式必须是post原始是因为post安全
                    request.setSysMethod(MethodType.POST);
                    //调用阿里大于发送短信的域名
                    request.setSysDomain("dysmsapi.aliyuncs.com");
                    //版本号
                    request.setSysVersion("2017-05-25");
                    //发送短信的功能
                    request.setSysAction("SendSms");
                    //接收放手机号
                    request.putQueryParameter("PhoneNumbers",str);
                        System.out.println(str);
                    //签名
                    request.putQueryParameter("SignName","东科创想");
                    //短信模板
                    request.putQueryParameter("TemplateCode", "SMS_162522027");
                    String numeric = RandomStringUtils.randomNumeric(6);
                    request.putQueryParameter("TemplateParam", "{'sms_code':'"+numeric+"'}");
                        System.out.println(numeric);
                        redisTemplate.setKeySerializer(new StringRedisSerializer());
                        ValueOperations valueOperations = redisTemplate.opsForValue();
                        valueOperations.set(str,numeric, 60, TimeUnit.SECONDS);

 //                      StringUtils.isBlank()  判断非空的时候用
                        CommonResponse response = client.getCommonResponse(request);
                        System.out.println(response.getData());
                    } catch (ServerException e) {
                        e.printStackTrace();
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }catch (Exception e){
                        e.printStackTrace();
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
    }
    @Override
    public void onMessage(String s) {
        System.out.println(s);
    }
}
